﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace ETRoboConTrackingSLServer
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ETRoboConMgmtSL// : IETRoboConMgmtSL
    {
        [OperationContract]
        public bool GetContestInfos(out ContestInfoSL[] contests)
        {
            bool result = true;
            var results = new List<ContestInfoSL>();
            using (var db = new ETRoboConTrackingSystemServer.ETRCGoVADBEntities())
            {
                var q = from c in db.ContestSet select c;
                foreach (var c in q)
                {
                    results.Add(new ContestInfoSL() { ContestId = c.ContestId, ContestName = c.Name, Year = c.Year });
                }
            }
            contests = results.ToArray();
            return result;
        }

        [OperationContract]
        public bool GetGameInfos(int contestId, out GameInfoSL[] games)
        {
            bool result = false;
            var results = new List<GameInfoSL>();
            using (var db = new ETRoboConTrackingSystemServer.ETRCGoVADBEntities())
            {
                var q = from g in db.GameSet
                        where g.Contest_ContestId == contestId
                        orderby g.Name
                        select g;
                if (q.Count() > 0)
                {
                    result = true;
                    foreach (var g in q)
                    {
                        results.Add(new GameInfoSL() { GameId = g.GameId, GameName = g.Name });
                    }
                }
            }
            games = results.ToArray();
            return result;
        }

        [OperationContract]
        public bool GetEntryTeamResults(int gameId, bool isTotalResultPoint, int? round, out EntryTeamResultSL[] results)
        {
            bool result = false;
            var etrs = new List<EntryTeamResultSL>();
            using (var db = new ETRoboConTrackingSystemServer.ETRCGoVADBEntities())
            {
                var games = from g in db.GameSet
                            where g.GameId == gameId
                            select g;
                if (games.Count() > 0)
                {
                    result = true;
                    var q = from et in db.EntryTeamSet
                            where et.Game_GameId == gameId
                            orderby et.Bib
                            select et;
                    if (isTotalResultPoint)
                    {
                        foreach (var et in q)
                        {
                            etrs.Add(new EntryTeamResultSL()
                            {
                                Bib = et.Bib,
                                TeamId = et.TeamSet.TeamId,
                                TeamName = et.TeamSet.Name,
                                Rank = et.Rank,
                                ResultTime = et.RaceTotalPoint
                            });
                        }
                    }
                    else
                    {
                        foreach (var et in q)
                        {
                            var trs = from tr in et.TurnRunnerSet
                                      where tr.TurnSet.RoundSet.RoundNo == round
                                      select tr;
                            int? resultTime = null;
                            if (trs.Count() > 0)
                            {
                                resultTime = trs.First().ResultPoint;
                            }
                            etrs.Add(new EntryTeamResultSL()
                            {
                                Bib = et.Bib,
                                TeamId = et.TeamSet.TeamId,
                                TeamName = et.TeamSet.Name,
                                Rank = et.Rank,
                                ResultTime = resultTime
                            });
                        }                        
                    }
                }
            }
            results = etrs.ToArray();
            return result;
        }
    }
}
